package com.kehutong.wwin.service.service;

import com.kehutong.common.util.Token;
import com.kehutong.wwin.service.util.WwInHttpClient;
import com.kehutong.wxapi.request.BatchGetByUserRequest;
import com.kehutong.wxapi.request.DepartmentListRequest;
import com.kehutong.wxapi.request.FollowUserListRequest;
import com.kehutong.wxapi.request.UserListRequest;
import com.kehutong.wxapi.response.BatchGetByUserResponse;
import com.kehutong.wxapi.response.DepartmentListResponse;
import com.kehutong.wxapi.response.FollowUserListResponse;
import com.kehutong.wxapi.response.UserListResponse;
import com.kehutong.wxapi.service.DepartmentService;
import com.kehutong.wxapi.service.ExternalContactService;
import com.kehutong.wxapi.service.UserService;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;

import java.util.ArrayList;
import java.util.List;

/**
 * 企业微信接口测试
 *
 * @author liuzhen(liuxing521a@163.com)
 * @createTime 2020年7月20日下午6:16:25
 */
public class WxApiService {
	private static final Logger logger= LoggerFactory.getLogger(WxApiService.class);

	@Inject
	private WwInHttpClient httpClient;
	@Inject
	private JdbcSession jdbcSession;

	@Inject
	private DepartmentService departmentService;

    @Inject
	private UserService userService;

	@Inject
	private ExternalContactService externalContactService;


	public void wxApiTest(Token token) throws Exception{
		  DepartmentListRequest departmentListRequest=new DepartmentListRequest();
		departmentListRequest.setAccessToken(httpClient.getEmployeeAccessToken(token));
		DepartmentListResponse res=departmentService.departmentList(departmentListRequest);
		List<DepartmentListResponse.DepartmentDTO> list=res.getDepartment();
		list.forEach(x->{
			logger.info("获取部门列表信息-----------------{}",JSONObject.toJSON(x));
		});



		UserListRequest userListRequest=new UserListRequest();
		userListRequest.setAccessToken(httpClient.getContactAccessToken(token));
		userListRequest.setDepartmentId(1);
		userListRequest.setDepartmentId(1);
		UserListResponse userListResponse=userService.userList(userListRequest);
		List<UserListResponse.UserlistDTO> userList=userListResponse.getUserlist();
		userList.forEach(x->{
			logger.info("获取员工列表信息-----------------{}",JSONObject.toJSON(x));
		});

		String accessToken = httpClient.getContactAccessToken(token);
		FollowUserListRequest followUserListRequest = new FollowUserListRequest();
		followUserListRequest.setAccessToken(accessToken);
		FollowUserListResponse followUserListResponse = externalContactService.getFollowUserList(followUserListRequest);

		List<String> followUserList = followUserListResponse.getFollowUser();
		for (String followUser : followUserList) {
			List<BatchGetByUserResponse.ExternalContactListDTO> externalContactList = getExternalContactList(token, followUser);
			externalContactList.forEach(x->{
				logger.info("获取外部联系人列表信息-----------------{}",JSONObject.toJSON(x));
			});

		}

	}
	/**
	 * 批量获取客户详情
	 *
	 * @param token  用户信息
	 * @param userid 企业成员的userid，字符串类型
	 * @return 客户详情集合
	 * @throws Exception 异常
	 */
	private List<BatchGetByUserResponse.ExternalContactListDTO> getExternalContactList(Token token, String userid) throws Exception {
		String accessToken = httpClient.getContactAccessToken(token);
		List<BatchGetByUserResponse.ExternalContactListDTO> externalContactList = new ArrayList<>();
		BatchGetByUserRequest batchGetByUserRequest = new BatchGetByUserRequest();
		batchGetByUserRequest.setAccessToken(accessToken);
		batchGetByUserRequest.setUserid(userid);
		batchGetByUserRequest.setLimit(100);
		batchGetByUser(externalContactList, batchGetByUserRequest);
		return externalContactList;
	}

	/**
	 * 递归请求批量获取客户详情接口
	 *
	 * @param externalContactList   客户的基本信息集合
	 * @param batchGetByUserRequest 批量获取客户详情请求对象
	 * @throws Exception 异常
	 */
	private void batchGetByUser(List<BatchGetByUserResponse.ExternalContactListDTO> externalContactList, BatchGetByUserRequest batchGetByUserRequest) throws Exception {
		if (Objects.nonNull(batchGetByUserRequest)) {
			BatchGetByUserResponse batchGetByUserResponse = externalContactService.batchGetByUser(batchGetByUserRequest);
			if (batchGetByUserResponse.getErrcode() == 0) {
				List<BatchGetByUserResponse.ExternalContactListDTO> reqExternalContactList = batchGetByUserResponse.getExternalContactList();
				if (Objects.nonEmpty(reqExternalContactList)) {
					externalContactList.addAll(reqExternalContactList);
					if(Objects.nonEmpty(batchGetByUserResponse.getNextCursor())){
						batchGetByUserRequest.setCursor(batchGetByUserResponse.getNextCursor());
						batchGetByUser(externalContactList, batchGetByUserRequest);
					}
				}
			}

		}
	}
}
